import type {
  BaseStyleConfig,
  IPersonalizationItemConfig,
  StyleConfigKey,
} from '../../../../types/interfaces'
import { TextInput } from '../../../../components/text_input'
import { UserPref } from '../../../../lib/abstract'
import { VALID_RESULT } from '../../../../types/constants'

export class LoadingPhrases extends UserPref {
  readonly id: string = 'loading_phrases'
  readonly name: string = 'LoadingPhrases'
  readonly styleConfigKey: StyleConfigKey = 'loadingPhrases'

  readonly cssProperty: string = ''
  readonly cssValueSuffix: string = ''
  // 由于文本输入使用的是失焦事件和回车事件，不会短时间高频触发事件，所以不需要防抖保存处理，故置为0
  readonly debouncedSaveTimeout: number = 0

  // 域名隔离控制
  useDomainIsolation: boolean = false

  constructor(config: IPersonalizationItemConfig) {
    super(config)
  }

  createComponent(config: BaseStyleConfig, value: string, _mode: string, debouncedSave: (() => void) & { flush: () => void }): TextInput {
    const id = this.generateId(config.styleVar)
    return new TextInput({
      id,
      label: '加载提示',
      type: 'text',
      placeholder: `输入自定义加载提示，刷新后生效`,
      value,
      onChange: (value) => {
        this.cachedValues.set(id, String(value))
        debouncedSave()
      },
    })
  }

  // 填写内容复杂，暂不需要额外的校验逻辑
  handleCheck(_value: string): { status: ValidResultType, error: string } {
    // 支持输入为空或长度为0
    return { status: VALID_RESULT.VALID.value, error: '' }
  }

  applyStyles(): void {}
}
